<?php
namespace plugins\order\controller;

use cmf\controller\ApiPluginBaseController;
use think\Db;
use plugins\order\model\OrderModel;
use plugins\order\model\OrderLogModel;
use plugins\logistics\model\LogisticsModel;
use plugins\order\model\OrderRefundModel;
use plugins\order\model\OrderGoodsListModel;

//此处放置订单编辑方法接口
class ApiEditOrderController extends ApiPluginBaseController
{

	//取消订单 6=>'已取消'
	public function cancelOrder()
	{
		$post = $this->request->post();
		$user = $this->getIdentity();
		checkParam(['order_num'=>['name'=>'订单编号']],$post);
		$order_num = $post['order_num'];

		$order = (new OrderModel)->get(['order_num'=>$order_num,'member_id'=>$user->uid]);

		if($order == null){
			return api_json_echo(false,'错误的请求，请重试');
		}

		if($order->order_status != 1){
			return api_json_echo(false,'当前订单状态无法取消');
		}

		$result = $order->save(['order_status'=>6]);

		if($result){
			return api_json_echo(true,'取消完成');
		}
		return api_json_echo(false,'取消失败,该订单不存在');

	}

	// 7=>'已删除',6=>'已取消'
	//删除订单
	public function delOrder()
	{
		$post = $this->request->post();
		$user = $this->getIdentity();

		checkParam(['order_num'=>['name'=>'订单编号']],$post);

		$order_num = $post['order_num'];

		$order = (new OrderModel)->get(['order_num'=>$order_num,'member_id'=>$user->uid]);

		if($order == null){
			return api_json_echo(false,'错误的请求，请重试');
		}

		if($order->order_status != 4 && $order->order_status != 6 && $order->order_status != 5 && $order->order_status != 10){
			return api_json_echo(false,'该订单状态无法删除！');
		}		

		$result = $order->save(['order_status'=>7]);

		if($result){
			return api_json_echo(true,'删除完成');
		}
		return api_json_echo(false,'删除失败');

	}

	//确认收货
	public function confirmReceipt()
	{
		if($this->request->isPost()){
			$post = $this->request->post();
			checkParam(['order_num'=>['name'=>'订单编号']],$post);
			$order = ( new OrderModel)->get(['order_num'=>$post['order_num']]);
			if($order == null){
				return api_json_echo(false,'订单不存在！');
			}
			if($order->order_status != 3){
				return api_json_echo(false,'该订单状态不是待收货，无法进行该项操作');
			}

			$return = $order->save(['order_status'=>4]);
			if($return){
				//模型内方法处理
				$order->confirmReceipt();

				( new OrderLogModel )->createLog(13,$post['order_num'],null,1);
				return api_json_echo(true,'订单状态修改完成');
			}
		}

		return api_json_echo(false,'错误的引用');
	}

	//提醒发货
	public function RemindDelivery()
	{
		$post = $this->request->post();
		$user = $this->getIdentity();

		checkParam(['order_num'=>['name'=>'订单编号']],$post);
		
		$order = ( new OrderModel )->get(['order_num'=>$post['order_num'],'member_id'=>$user->uid,'order_status'=>['in',['1','2','3','4']]]);
		if($order == null){
			return api_json_echo(false,'您的账户下没有该订单，请重试');
		}


		if($order->order_status != 2){
			return api_json_echo(false,'该订单未在已发货状态，请重试');
		}

		//提醒发货操作

		return api_json_echo(true,'提醒发货成功');

	}

	//订单申请退款
	public function orderRefund()
	{
		$param = $this->request->param();
		$info = $this->getIdentity();
		checkParam(['order_num'=>['name'=>'订单号'],'cause'=>['name'=>'退款原因']],$param);

		//退款原因   退款说明   凭证
		//cause remark voucher

		//订单检测
		$order = ( new OrderModel )->get(['member_id'=>$info->uid,'order_num'=>$param['order_num']]);

		if($order == null){
			return api_json_echo(false,'您的账户下没有该订单，请重试');
		}

		//状态检测
		if($order->order_status != 2 && $order->order_status != 3){
			return api_json_echo(false,'订单当前状态无法申请退款！');
		}


		if($order->order_refund == 1 || $order->order_refund == 2){
			return api_json_echo(false,'该订单已在退款申请中或退款已成功');
		}
		//记录原有状态
		$order_refund = $order->order_refund;
		//检测结束修改订单状态
		$order->order_refund = 2;
		$result = $order->save();
		$result = true;
		if($result){

			//退款请求记录增加
			$param['cause'];
			$param['remark'];
			$param['voucher'] = autoHtmlspecialcharsDecode($param['voucher']);
			$param['voucher'] = json_decode($param['voucher'],true);
			$voucher = [];
			if(!empty($param['voucher'])){
				foreach ($param['voucher'] as $key => $value) {
					$voucher[] = fileUploadBase64($value,'order','refund');
				}				
			}
			$param['voucher'] = json_encode($voucher);
			$param['time'] = time();
			
			if($order_refund == 4){
				( new OrderRefundModel)->allowField(true)->save($param);
			}

			if($order_refund == 3){
				$order_refund = ( new OrderRefundModel )->get(['order_num'=>$order->order_num]);
				$order_refund->allowField(true)->isUpdate(true)->save($param);
			}

			return api_json_echo(true,'您的退款审核已提交，请等待后台处理');
		}
		return api_json_echo(false,'操作失败，请刷新页面重试');

	}

	//订单退款申请取消
	public function orderRefundCancel()
	{
		$param = $this->request->param();
		$info = $this->getIdentity();
		checkParam(['order_num'=>['name'=>'订单号']],$param);

		//订单检测
		$order = ( new OrderModel )->get(['member_id'=>$info->uid,'order_num'=>$param['order_num']]);

		if($order == null){
			return api_json_echo(false,'您的账户下没有该订单，请重试');
		}

		if($order->order_refund == 1){
			return api_json_echo(false,'该订单申请退款已成功,无法取消');
		}

		if($order->order_refund == 4){
			return api_json_echo(false,'该订单未申请退款,无法取消');
		}

		$order->order_refund = 4;
		$result = $order->save();
		if($result){
			//删除请求
			( new OrderRefundModel )->where(['order_num'=>$order->order_num])->delete();
			return api_json_echo(true,'订单的退款申请已取消');
		}
		return api_json_echo(false,'操作失败，请刷新页面重试');
	}


	//订单退款申请列表
	public function orderRefundList()
	{
		$param = $this->request->param();
		$info = $this->getIdentity();
		empty($param['page']) ? $page = 1 : $page = $param['page'];
		empty($param['pagesize']) ? $pagesize = 10 : $pagesize = $param['pagesize'];

		$where['member_id'] = $info->uid;
		$where['order_refund'] = ['in',['1','2','3']];
		$where['order_status'] = ['neq',4];

		//得到退款申请列表
		$list = ( new OrderModel )
				->where($where)
				->field('pay_order',true)
				->order('submit_time desc,field(order_refund,3,2,1)')
				->paginate($pagesize,false,['page'=>$page]);


		foreach ($list as $key => $value) {
			//订单的商品记录
			$goods_list = ( new OrderGoodsListModel )
						->field('member_id',true)
						->where(['order_num'=>$value->order_num])->select();
			if($goods_list != null){
				$goods_list = $goods_list->toArray();
			}
			foreach ($goods_list as $k => &$v) {
				$v['order_goods_img'] = zy_domain().$v['order_goods_img'];				
			}

			$value->goods = $goods_list;

			//退款申请记录
			$refund = ( new OrderRefundModel )->get(['order_num'=>$value->order_num]);
			//退款申请时间
			$refund->time = date('Y-m-d H:i:s',$refund->time);
			$voucher = json_decode($refund->voucher,true);
			foreach ($voucher as $k => &$v) {
				$v = zy_domain().$v;
			}
			$refund->voucher = $voucher;
			$value->refund = $refund;
			//支付时间
			$value->paytime = date('Y-m-d H:i:s',$value->paytime);
			//订单创建时间
			$value->submit_time = date('Y-m-d H:i:s',$value->submit_time);

			//支付方法
			switch ($value->money_type) {
				case 'balance':
					$value->money_type = '余额';
					break;
				case 'wechatpay':
					$value->money_type = '微信';
					break;
				case 'alipay':
					$value->money_type = '支付宝';
					break;									
				default:
					# code...
					break;
			}

		}
		return api_json_echo(true,'',$list);
	}

	function string2array($data) {
		$data = trim($data);
		if($data == '') return array();
		if(strpos($data, 'array')===0){
			@eval("\$array = $data;");
		}else{
			if(strpos($data, '{\\')===0) $data = stripslashes($data);
			$array=json_decode($data,true);
		}
		return $array;
	}	

}